package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.junzhiit.nengji.cms.service.ChainSkuItemsService;
import com.junzhiit.nengji.cms.web.exception.SkuItemsNameIsExistException;
import com.junzhiit.nengji.cms.web.exception.SkuItemsNotFoundException;
import com.junzhiit.nengji.cms.web.exception.SkuNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.ChainSkuItemsAddVO;
import com.junzhiit.nengji.cms.web.vo.ChainSkuItemsEditVO;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.dao.ChainSkuDAO;
import com.junzhiit.nengji.dao.ChainSkuItemsDAO;
import com.junzhiit.nengji.model.ChainSku;
import com.junzhiit.nengji.model.ChainSkuItems;
import com.junzhiit.nengji.model.SkuItems;

@Service
public class ChainSkuItemsServiceImpl extends BaseServiceImpl implements ChainSkuItemsService{

	@Autowired
	private ChainSkuItemsDAO chainSkuItemsDAO;
	
	@Autowired
	private ChainSkuDAO chainSkuDAO;
	
	
	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, String chainSkuId) {
		List<SkuItems> list = chainSkuItemsDAO.findAll(chainSkuId, dtp.getStart(), dtp.getLength());
		int totalCount = chainSkuItemsDAO.getTotalCount(chainSkuId);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO add(ChainSkuItemsAddVO chainSkuItemsAddVO) {
		ResultVO result = new ResultVO();

		ChainSku chainSku = chainSkuDAO.query(chainSkuItemsAddVO.getChainSkuId());
		if (chainSku == null) {
			throw new SkuNotFoundException("该顶级SKU未找到");
		}

		int count = chainSkuItemsDAO.queryNameIsExit(chainSkuItemsAddVO.getChainSkuId(), chainSkuItemsAddVO.getStandard());

		if (count > 0) {
			throw new SkuItemsNameIsExistException("该SKU规格已经存在");
		}

		ChainSkuItems chainSkuItems = new ChainSkuItems();
		BeanUtils.copyProperties(chainSkuItemsAddVO, chainSkuItems);

		ChainSkuItems data = chainSkuItemsDAO.insert(chainSkuItems);
		result.setData(data);
		return result;
	}

	@Override
	public ResultVO delete(String id) {
		ResultVO result = new ResultVO();
		boolean data = chainSkuItemsDAO.updateToDelete(id);
		result.setData(data);
		return null;
	}

	@Override
	public ResultVO batchDelete(String[] ids) {
		ResultVO result = new ResultVO();
		boolean data = chainSkuItemsDAO.batchUpdateToDelete(ids);
		result.setData(data);
		return null;
	}

	@Transactional
	@Override
	public ResultVO update(ChainSkuItemsEditVO chainSkuItemsEditVO) {
		
		ResultVO result = new ResultVO();

		ChainSkuItems chainSkuItems = chainSkuItemsDAO.query(chainSkuItemsEditVO.getId());
		if (chainSkuItems == null) {
			throw new SkuItemsNotFoundException("sku规格未找到");
		}
		
		if(chainSkuItems.getStandard().equals(chainSkuItemsEditVO.getStandard())){
			return result;
		}
		
		int count = chainSkuItemsDAO.queryNameIsExit(chainSkuItems.getChainSkuId(), chainSkuItemsEditVO.getStandard());
		if (count > 0) {
			throw new SkuItemsNameIsExistException("该SKU规格已经存在");
		}
		chainSkuItems.setStandard(chainSkuItemsEditVO.getStandard());
		boolean flag = chainSkuItemsDAO.update(chainSkuItems);

		result.setData(flag);
		return result;
	}

	@Override
	public ResultVO getAll(String chainSkuId) {
		ResultVO result=new ResultVO();
		List<SkuItems> data=chainSkuItemsDAO.getAll(chainSkuId);
		result.setData(data);
		return result;
	}

	@Override
	public ResultVO queryGoodsAboutSkuItemsCount(String chainSkuItemsId) {
		ResultVO result=new ResultVO();
		int data=chainSkuItemsDAO.queryGoodsAboutSkuItemsCount(chainSkuItemsId);
		result.setData(data);
		return result;
	}

}
